package Queue1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-05-12
 * Time: 15:45
 */
public class MyQueue {
     class Node{
         private int val;
         private Node next;

         public Node(int val){
             this.val = val;
         }
     }
     private int usedSize;
     private Node front;
     private Node rear;

     //入队---->尾插入队
     public void offer(int val){
         Node node = new Node(val);
         if(front==null){
             front = node;
             rear = node;
         }else {
             rear.next = node;
             rear = node;
         }
         this.usedSize++;
     }

     public int poll(){
         if(isEmpty()){
             throw  new RuntimeException("队列为空！！！");
         }
         int val = front.val;
         front=front.next;
         if(front==null){
             rear=null;
         }
         this.usedSize--;
         return val;
     }


     public int peek(){
         if(isEmpty()){
             throw  new RuntimeException("队列为空！！！");
         }
         return front.val;
     }

     public Boolean isEmpty(){
         return this.usedSize==0;
     }

     public int size(){
         return this.usedSize;
     }

}
